﻿using Framework.Common.Paged;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework.Common
{
    public static class PagedListExtension
    {
        //public static dynamic ToGridData<T>(this StaticPagedList<T> value)
        //{
        //    var gridData = new
        //    {
        //        currpage = value.PageNumber,
        //        totalpages = Math.Ceiling(value.TotalItemCount / (decimal)value.PageSize),
        //        totalrecords = value.TotalItemCount,
        //        griddata = value.ToList()
        //    };
        //    return gridData;
        //}
        public static CustomPagedList<T> ToStaticPagedList<T>(this IQueryable<T> query, int pageNo, int pageSize)
        {
            var sourcePagedList = query.ToPagedList(pageNo, pageSize);
            var sourceList = sourcePagedList.ToList();
            var destPagedList = new StaticPagedList<T>(sourceList, sourcePagedList);
            var list = new CustomPagedList<T>(destPagedList);

            return list;
        }

        public static CustomPagedList<TTo> ToCustomPagedList<TFrom, TTo>(this CustomPagedList<TFrom> source, Func<TFrom, TTo> converter)
        {
            return CustomPagedList<TTo>.FromIPagedList(source, converter);
        }
        
    }
}
